﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InternalManagementSystem.DAO.Models;
using InternalManagementSystem.DAO;
using System.Data;

/// <summary>
///UserBLL 的摘要说明
/// </summary>
public class UserBLL
{
    Permission permission = null;
    public UserBLL() { }
	public UserBLL(Permission permission)
	{
        this.permission = permission;
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 编辑员工的权限身份。
    /// </summary>
    /// <param name="userid"></param>
    /// <param name="role"></param>
    /// <returns></returns>
    public bool UpdateUserRole(int userid, EUserRole role)
    {
        if (!this.permission.IsUserRoleEdit)
        {
            return true;
        }
        StaffDAO dao = new StaffDAO();
        Staff user = dao.LoadUser(userid);
        user.RoleID = (int)role;
        dao.UpdateUser(user);
        return true;
    }

    /// <summary>
    /// 重置一个用户
    /// </summary>
    /// <param name="userid"></param>
    public void AutoRecharge(int userid)
    {
        if (!this.permission.IsAutoRechargEdit)
        {
            return;
        }
        StaffDAO dao = new StaffDAO();
        Staff user = dao.LoadUser(userid);
        if (user.CurrentIntegral != 6)
        {
            user.CurrentIntegral = 6;
            dao.UpdateUser(user);
        }
    }

    /// <summary>
    /// 重置一个用户
    /// </summary>
    /// <param name="user"></param>
    public void AutoRecharge(Staff user)
    {
        if (!this.permission.IsAutoRechargEdit)
        {
            return;
        }
        if (user.CurrentIntegral != 6)
        {
            StaffDAO dao = new StaffDAO();
            user.CurrentIntegral = 6;
            dao.UpdateUser(user);
        }
    }

    /// <summary>
    /// 重置公司所有用户
    /// </summary>
    /// <param name="companyid"></param>
    public void AutoRechargeInCompany(int companyid)
    {
        if (!this.permission.IsAutoRechargEdit)
        {
            return;
        }
        StaffDAO dao = new StaffDAO();
        UserCollection col = dao.LoadAllUsers("CompanyID = " + companyid.ToString(), string.Empty);
        for (int i = 0; i < col.Count; i++)
        {
            AutoRecharge(col[i]);
        }

    }

    /// <summary>
    /// 通过设置员工类型来确定警戒线
    /// </summary>
    public void SetUserType(int userid, int usertypeid)
    {
        if (!this.permission.IsUserTypeEdit)
        {
            return;
        }
        StaffDAO dao = new StaffDAO();
        Staff user = dao.LoadUser(userid);
        user.UserTypeID = usertypeid;
        dao.UpdateUser(user);
    }

    /// <summary>
    /// 员工登录进来的话，就提示自己的警戒线
    /// 是否超出警戒线
    /// </summary>
    /// <param name="usertypeid">用户类型id</param>
    /// <param name="currentIntegral">当前积分</param>
    /// <returns></returns>
    public bool IsOverGuardLine(int usertypeid, int currentIntegral)
    {
         bool result = false;
        UserTypeDAO typeDao = new UserTypeDAO();
        UserType userType = typeDao.LoadUserType(usertypeid);
        if ( currentIntegral <= userType.GuardLine)
        {
            result = true;
        }

        return result;
    }

    #region 公司员工信息自由增加和删除
    /// <summary>
    /// 获取用户
    /// </summary>
    /// <param name="userid"></param>
    /// <returns></returns>
    public Staff GetUser(int userid)
    {
        StaffDAO dao = new StaffDAO();
        return dao.LoadUser(userid);
    }

    /// <summary>
    /// 获取用户列表
    /// </summary>
    /// <returns></returns>
    public IList<Staff> GetAllUser()
    {
        StaffDAO dao = new StaffDAO();
        return dao.LoadAllUsers().Cast<Staff>().ToList();
    }



    /// <summary>
    /// 新增用户
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    public bool AddUser(Staff user)
    {
        bool result = false;

        if (!this.permission.IsUserEdit)
        {
            return result;
        }
        
        try
        {
            StaffDAO dao = new StaffDAO();

            string query = "UserName = '" + user.UserName + "'";

            if (dao.LoadAllUsers(query, "").Count == 0)
            {
                dao.InsertUser(user);
                result = true;
            }
        }
        catch
        {
        }

        return result;
    }

    /// <summary>
    /// 更新用户
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    public bool UpdateUser(Staff user)
    {
        if (!this.permission.IsUserEdit)
        {
            return true;
        }
        bool result = true;
        try
        {
            StaffDAO dao = new StaffDAO();
            dao.UpdateUser(user);
        }
        catch
        {
            result = false;
        }

        return result;
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="userid"></param>
    /// <returns></returns>
    public bool DeleteUser(int userid)
    {
        if (!this.permission.IsUserEdit)
        {
            return true;
        }
        bool result = true;
        try
        {
            StaffDAO dao = new StaffDAO();
            UOCDAO uocdao = new UOCDAO();

            //删除员工相关uoc
            UOCCollection uoccol = uocdao.LoadAllUOCs("userid = " + userid.ToString(), "");
            foreach (UOC uoc in uoccol)
            {
                uocdao.DeleteUOC(uoc.ID);
            }

            dao.DeleteUser(userid);
        }
        catch
        {
            result = false;
        }

        return result;
    }

    #endregion

    /// <summary>
    /// 获取视图数据集
    /// </summary>
    /// <returns></returns>
    public DataSet GetAllUserView()
    {
        DBHelper helper = new DBHelper();
        string sql = "SELECT * FROM [V_User]";
        return helper.ExecuteQuerySQL(sql);
    }

    public DataSet GetAllUserView(int companyID)
    {
        DBHelper helper = new DBHelper();
        string sql = "SELECT * FROM [V_User] where [CompanyID] = " + companyID.ToString();
        return helper.ExecuteQuerySQL(sql);
    }

    public DataSet GetUserView(int userID)
    {
        DBHelper helper = new DBHelper();
        string sql = "SELECT * FROM [V_User] where [UserID] = " + userID.ToString();
        return helper.ExecuteQuerySQL(sql);
    }

    /// <summary>
    /// 获取用户列表
    /// </summary>
    /// <returns></returns>
    public IList<Staff> GetLeaderRoleUser()
    {
        StaffDAO dao = new StaffDAO();
        return dao.LoadAllUsers(" RoleID = 3 ", string.Empty).Cast<Staff>().ToList();
    }

    public void ExportToExcel()
    {
        //待实现
    }

    /// <summary>
    /// 用户登录
    /// </summary>
    /// <param name="name">用户名</param>
    /// <param name="password"> 密码</param>
    /// <returns>user</returns>
    public Staff UserLogin(string name,string password)
    {
        Staff user = null;
        UserCollection userCollection = new UserCollection();
        try
        {
            string query = "UserName = '" + name + "' and password = '" + password + "'";
            StaffDAO userdao = new StaffDAO();
            userCollection =  userdao.LoadAllUsers(query,"");
        }
        catch
        {
            return user;
        }
        if (userCollection.Count>0)
        {
            user = userCollection[0];
        }

        return user;
    }

    public bool ChangePassWord(int userid,string oldpsw, string newpsw)
    {
        StaffDAO userdao = new StaffDAO();
        Staff user = userdao.LoadUser(userid);
        if (user.Password.Trim() == oldpsw)
        {
            user.Password = newpsw;
            userdao.UpdateUser(user);
            return true;
        }
        else
        {
            return false;
        }
    }


    public void DoDeduct(int violationid,int userid, int userIntegral, int companyIntegral,string reason)
    {
        if (permission.IsDeductOperate)
        {
            if (userIntegral > 0)
            {
                StaffDAO sdao = new StaffDAO();
                DeductDAO ddao = new DeductDAO();
                Staff user = sdao.LoadUser(userid);

                if (user != null)
                {
                    //扣员工
                    if (user.CurrentIntegral > userIntegral)
                    {
                        user.CurrentIntegral = user.CurrentIntegral - userIntegral;
                     
                        sdao.UpdateUser(user);

                        Deduct pDeduct = new Deduct();
                        pDeduct.DeductIntegral = userIntegral;
                        pDeduct.DeductType = 0;
                        pDeduct.Reason = reason;
                        pDeduct.ViolationNID = violationid;
                        pDeduct.UserID = userid;
                        ddao.InsertDeduct(pDeduct);
                    }

                    //扣公司
                    if (companyIntegral > 0)
                    {
                        CompanyDAO cdao = new CompanyDAO();
                        
                        Company company = cdao.LoadCompany(user.CompanyID);

                        if (company.CurrentIntegral > companyIntegral)
                        {
                            company.CurrentIntegral = company.CurrentIntegral - companyIntegral;

                            cdao.UpdateCompany(company);

                            Deduct pDeduct2 = new Deduct();
                            pDeduct2.DeductIntegral = companyIntegral;
                            pDeduct2.DeductType = 1;
                            pDeduct2.Reason = reason;
                            pDeduct2.ViolationNID = violationid;
                            pDeduct2.UserID = user.CompanyID;
                            ddao.InsertDeduct(pDeduct2);
                        }
                    }
                }
            }
        }
        
    }
}
